/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
#ifndef __MuRoMainFrame__
#define __MuRoMainFrame__

/**
@file
Subclass of MainFrame, which is generated by wxFormBuilder.
*/

#include "Component.hpp"
#include "MaCICtrlClient.hpp"
#include "MuRo_Frames.h"
//#include "Core.hpp"
#include "GLDisplay.hpp"
#include "MuRoHelpFrame.h"
#include "MuRoSettingsFrame.h"
#include "Device.hpp"
#include "Group.hpp"

BEGIN_DECLARE_EVENT_TYPES()
DECLARE_EVENT_TYPE(MY_EVT_UPDATE_DISPLAYS, -1)
END_DECLARE_EVENT_TYPES()

using namespace MuRo;

class CDeviceTreeItemData : public wxTreeItemData
{
  public:
    CDeviceTreeItemData(CDevice *device)
    :device(device){}
    
    CDevice *device;
};

class CChoiceItemData : public wxClientData
{
  public:
    CChoiceItemData(CDevice *device)
  :device(device){}
    
    CDevice *device;
};

/** Implementing MainFrame */
class MuRoMainFrame : public MainFrame, 
                      public Gimbo::CComponent, 
                      private gim::CSync,
                      private gim::CThread
{
  DECLARE_EVENT_TABLE()
protected:
  
  wxMenuItem *menuItem_Devices;
  wxMenu *dummySub;
  
  MuRoHelpFrame helpFrame;
  MuRoSettingsFrame settingsFrame;
  
  // Handlers for MainFrame events.
  void OnExit( wxCommandEvent& event );
  void OnGrid( wxCommandEvent& event );
  void OnFollowMode( wxCommandEvent& event );
  void OnZoomIn( wxCommandEvent& event );
  void OnZoomOut( wxCommandEvent& event );
  
	void OnMiniview( wxCommandEvent& event );
	void OnHelp( wxCommandEvent& event );
        void OnSettings( wxCommandEvent& event );
	void OnOptions( wxCommandEvent& event );
  void OnPosition( wxCommandEvent& event );
  
  void OnObservationMode( wxCommandEvent& event );
  void OnManual( wxCommandEvent& event );
  void OnCoordinateDrive( wxCommandEvent& event );
  
  
  void OnPositionHistory( wxCommandEvent& event );
  void OnRangingHistory( wxCommandEvent& event );
  
  void OnMachineActivated( wxTreeEvent& event );
  void OnMachineSelected( wxTreeEvent& event );
  void OnDeviceActivated( wxTreeEvent& event );
  
  void OnMenuEvent(wxCommandEvent &e);
  
  void OnPositionChoice( wxCommandEvent& event );
  void OnManualChoice( wxCommandEvent& event );
  void OnCoordChoice( wxCommandEvent& event );
  
  //handlers for custom events
  void OnUpdateDisplays(wxCommandEvent& event);
  
  //CMuRoCore *MuRo;
  CGLDisplay glDisplay;
  CMachine* selectedMachine;
  CDevice* selectedDevice;
  wxTreeItemId selectedDeviceTreeItem;
  CGroup *rootGroup;
  MaCI::MaCICtrl::CMaCICtrlClient *MaCICtrlClient;
  wxTreeItemId rootItem;
  
public:
  
  /** Constructor */
	MuRoMainFrame( wxWindow* parent);//, CMuRoCore *MuRo );
  ~MuRoMainFrame();
  
  void OnChildFocus(wxChildFocusEvent &e);
  
  //Gimbo component callbacks
  void OnServiceAssignment(const Gimbo::TServiceAssignmentList &list);
  
  CMachine *GetSelectedMachine(){return selectedMachine;}
  
  void AddMachine(CMachine* machine);
  void RemoveMachine(CMachine* machine);
  
  void SelectMachine(std::string name);
    //selects the next machine in list
  void SelectNextMachine(bool previous=false);
  void DeselectCurrent();
  
  void SetPositionDisplay(float x, float y, float heading);
  //void SetSelectedUnit(CMachine *machine);
  
  CGLDisplay* GetGLDisplay();
  
  const MuRoSettingsFrame& GetSettingsFrame(){return settingsFrame;}

  std::map<std::string, CMachine*> machines;
private:
  bool skipSelectionEvent;
  static const int machinesMutex;
  static const wxColour enabledColour;
  static const wxColour notEnabledColour;
  bool isAcceptedByMe(const Gimbo::TServiceAssignment *si);
  void selectMachine(CMachine *machine);
  void enableDevice(CDevice *device);
  
  int ThreadFunction(const int aThreadNumber);
  
};

#endif // __MuRoMainFrame__
